// pages/withdrawalsRecord/withdrawalsRecord.js
var util = require('../../utils/util')
var page;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reach: 0, //总成交笔数
    income: 0, //总成交金额
    dealNum: 0, //当月成交笔数
    turnover: 0, //当月成交金额
    bill: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let dealNum = that.data.dealNum;
    let turnover = that.data.turnover;
    if (dealNum >= 10000 || turnover >= 99999.99) {
      that.setData({
        dealNum: "999+",
        turnover: '99999+'
      })
    } else {
      dealNum,
      turnover
    }
    this.setData({
      reach: options.reach,
      income: options.income,
      dealNum: options.dealNum,
      turnover: options.turnover
    })
    page = 1;
    if (wx.getStorageSync('openid')) {
      let openid = wx.getStorageSync('openid')
      that.seek(openid)
    } else {
      wx.cloud.callFunction({
        name: 'login',
      }).then(res => {
        let openid = res.result.openid;
        that.seek(openid)
      })
    }
  },
  seek: function (openid) {
    var that = this;
    util.request('/withdrawal/bill/with', {
      openid: openid,
      page: page,
      size: 50
    }, 'GET').then(res => {
      console.log(res)
      that.setData({
        bill: that.data.bill.concat(res.data)
      })
    })
    let date = util.formatDate(new Date())
    util.request('/withdrawal/count', {
      openid: openid,
      date: date.substring(0, 7)
    }, 'GET').then(res => {
      console.log(res.data)
      that.setData({
        dealNum: res.data[0].reach,
        turnover: res.data[0].total//.toFixed(1)
      })
    })
    util.request('/withdrawal/count', {
      openid: openid
    }, 'GET').then(res => {
      console.log(res.data)
      that.setData({
        reach: res.data[0].reach,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    page = page + 1;
    this.seek(app.globalData.openid);
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})